﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KvoMvc.ServiceProxy;
using KvoMvc.BusinessEntity;

namespace KvoMvc.WebUI.Controllers
{
    public class PersonManagerController : Controller
    {
        //
        // GET: /PersonManager/

        public ActionResult Index()
        {
            var personList = SchoolManagerProxy.GetPersonList();
            return View(personList);
        }

        //
        // GET: /PersonManager/Details/5

        public ActionResult Details(int id)
        {
            Person person = SchoolManagerProxy.GetPersonById(id);
            return View(person);
        }

        //
        // GET: /PersonManager/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PersonManager/Create

        [HttpPost]
        public ActionResult Create(Person person)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int result = SchoolManagerProxy.CreatePerson(person);
                    return RedirectToAction("Index");
                }

                return View(person);
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /PersonManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Person person = SchoolManagerProxy.GetPersonById(id);
            return View(person);
        }

        //
        // POST: /PersonManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Person person)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    int result = SchoolManagerProxy.UpdatePerson(person);
                    return RedirectToAction("Index");
                }

                return View(person);
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PersonManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /PersonManager/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
